﻿using System;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Threading;
using Microsoft.Phone.Controls;

namespace Achievements
{
    public partial class AchievementPopup
    {
        private const int DisplayTime = 5;

        public static readonly DependencyProperty MyPropertyProperty =
            DependencyProperty.Register("MyProperty", typeof (Achievement), typeof (AchievementPopup),
                                        new PropertyMetadata(null));

        private readonly PhoneApplicationPage _parentPage;
        private readonly DispatcherTimer _timer = new DispatcherTimer();

        public AchievementPopup()
        {
            // Required to initialize variables
            InitializeComponent();

            Loaded += AchievementPopupLoaded;

            //update the width of the control based on the orientation, and the parent page's width.
            var frame = Application.Current.RootVisual as PhoneApplicationFrame;
            if (frame != null)
            {
                _parentPage = frame.Content as PhoneApplicationPage;
                if (_parentPage != null)
                {
                    _parentPage.OrientationChanged += (s, e) => UpdateControlWidth();
                    UpdateControlWidth();
                }
            }
        }

        /// <summary>
        /// Gets or sets the achievement.
        /// </summary>
        /// <value>
        /// The achievement.
        /// </value>
        public Achievement Achievement
        {
            get { return (Achievement) GetValue(MyPropertyProperty); }
            set { SetValue(MyPropertyProperty, value); }
        }

        /// <summary>
        /// Updates the width of the control.
        /// </summary>
        private void UpdateControlWidth()
        {
            if (_parentPage != null)
            {
                Width = _parentPage.ActualWidth;
            }
        }

        /// <summary>
        /// Called when the popup is loaded. Sets the timer which will hide the popup when it expires.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void AchievementPopupLoaded(object sender, RoutedEventArgs e)
        {
            _timer.Interval = TimeSpan.FromSeconds(DisplayTime);
            _timer.Tick += OnTimerOnTick;
            _timer.Start();
        }

        /// <summary>
        /// Called when The timer ticks, and the popup should be hidden.
        /// </summary>
        /// <param name="s">The s.</param>
        /// <param name="ee">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void OnTimerOnTick(object s, EventArgs ee)
        {
            var parent = Parent as Popup;
            if (parent != null)
            {
                parent.IsOpen = false;
            }
            _timer.Tick -= OnTimerOnTick;
            _timer.Stop();
        }
    }
}